package com.loklok.code.confuse.ui;

import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class OverwriteFileDialog extends DialogWrapper {

    private final String fileName;
    private JCheckBox applyToAllCheckBox;

    public OverwriteFileDialog(String fileName) {
        super(true); // 设置为模态对话框
        this.fileName = fileName;
        init();
        setTitle("file already exist");
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 提示信息
        JLabel messageLabel = new JLabel("file: " + fileName + " Already exists. Is it overwrite?");
        panel.add(messageLabel, BorderLayout.NORTH);

        // 复选框
        applyToAllCheckBox = new JCheckBox("Apply to all files");
        panel.add(applyToAllCheckBox, BorderLayout.SOUTH);

        return panel;
    }

    @Override
    protected JComponent createSouthPanel() {
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        // 覆盖按钮
        JButton overwriteButton = new JButton("overwrite");
        overwriteButton.addActionListener(e -> close(DialogWrapper.OK_EXIT_CODE));

        // 跳过按钮
        JButton skipButton = new JButton("skip");
        skipButton.addActionListener(e -> close(DialogWrapper.CANCEL_EXIT_CODE));

        panel.add(overwriteButton);
        panel.add(skipButton);

        return panel;
    }

    public boolean isApplyToAll() {
        return applyToAllCheckBox.isSelected();
    }
}